package com.itheima.sevrice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Employee;
import com.itheima.common.PageParam;
import com.itheima.dao.EmployeeDao;
import com.itheima.sevrice.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

@Service
@Transactional
public class EmployeeServiceImpl  implements EmployeeService {

    @Autowired(required = false)
    private EmployeeDao employeeDao;

    /**
     * 登录
     *
     * @param employee
     * @return
     */
    @Override
    public Employee login(Employee employee) {
        ////将页面提交的密码password 进行md5 加密处理
        String password = employee.getPassword();
        String pwdMD5 = DigestUtils.md5DigestAsHex(password.getBytes());

        //构建条件对象
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //追加条件
        lqw.eq(Employee::getUsername,employee.getUsername());
        lqw.eq(Employee::getPassword,pwdMD5);

        //4.1追加状态值的判定  1 表示为在使用中。
        lqw.eq(Employee::getStatus,1);

        //执行条件
        return   employeeDao.selectOne(lqw);
    }

    /**
     * 分页查询
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<Employee> findPage(PageParam pageParam) {
        //构建分页条件对象
        IPage<Employee> iPage=new Page<>(pageParam.getPage(),pageParam.getPageSize());
        //构建条件对象
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //追加条件
        lqw.like(pageParam.getName() !=null, Employee::getName,pageParam.getName());
        //执行条件
        return employeeDao.selectPage(iPage,lqw);
    }

    /**
     * 添加员工
     *
     * @param employee
     * @return
     */
    @Override
    public int add(Employee employee) {
        return employeeDao.insert(employee);
    }

    /**
     * 修改员工状态
     *
     * @param employee
     * @return
     */
    @Override
    public int update(Employee employee) {
       return employeeDao.updateById(employee);
    }

    /**
     * 根据员工id 查询员工
     * @param id
     * @return
     */
    @Override
    public Employee find(Long  id) {
        return employeeDao.selectById(id);
    }


}
